package com.turnengine.client.global.announcement.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.announcement.bean.AnnouncementSerializer;
import com.turnengine.client.global.announcement.bean.IAnnouncement;
import com.turnengine.client.global.game.enums.GameLevel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The Get Announcement List Data Serializer.
 */
public class GetAnnouncementListDataSerializer extends AbstractCommandDataSerializer<List<IAnnouncement>> implements IGetAnnouncementListDataSerializer {

	/**
	 * Creates a new Get Announcement List Data Serializer.
	 */
	public GetAnnouncementListDataSerializer() {
		super(GetAnnouncementList.COMMAND_ID);
	}

	@Override
	public List<IAnnouncement> readReturnValue(IDataReader reader) throws IOException {
		List<IAnnouncement> value = reader.readObjectCollection(new ArrayList<IAnnouncement>(), true, false, new AnnouncementSerializer());
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, List<IAnnouncement> value) throws IOException {
		writer.writeObjectCollection(value, true, false, new AnnouncementSerializer());
	}

	@Override
	public IExecutableCommand<List<IAnnouncement>> readRequest(IDataReader reader) throws IOException {

		// Read fields
		int gameLevelId = reader.readInt();
		GameLevel gameLevel = reader.readEnum(GameLevel.class, true);

		// Create object
		GetAnnouncementList object = new GetAnnouncementList();
		object.setGameLevelId(gameLevelId);
		object.setGameLevel(gameLevel);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<List<IAnnouncement>> argument) throws IOException {
		GetAnnouncementList object = (GetAnnouncementList) argument;

		// Get fields
		int gameLevelId = object.getGameLevelId();
		GameLevel gameLevel = object.getGameLevel();

		// Write fields
		writer.writeInt(gameLevelId);
		writer.writeEnum(gameLevel, true);
	}

	@Override
	public IExecutableCommandResponse<List<IAnnouncement>> newResponse() {
		return new GetAnnouncementListResponse();
	}
}
